package com.tengju.bff.interfaces.covercharge.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.tengju.bff.interfaces.covercharge.service.IServiceContractService;
import com.tengju.bff.interfaces.covercharge.service.NotifyService;
import com.tengju.bff.interfaces.covercharge.service.bestsign.BestSignClient;
import com.tengju.bff.interfaces.covercharge.util.HtmlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author shiliang
 * @date 2020.06.12 15:56
 * @description 上上签通知服务
 */
@Slf4j
@Service
public class NotifyBestSignServiceImpl implements NotifyService {

    /**
     * 签署合同通知
     */
    private static final String CONTRACT_SIGN = "signContract";
    /**
     * 申请证书通知
     */
    private static final String CERT_APPLY = "certApply";

    @Autowired
    private IServiceContractService contractService;

    @Resource
    private BestSignClient bestSignClient;

    @Override
    public HashMap signVerify(Object reqObj, HttpServletRequest request) {
        String rtick = request.getHeader("rtick");
        String sign = request.getHeader("sign");
        if (StringUtils.isBlank(rtick) || StringUtils.isBlank(sign)) {
            log.error("【通知-验签】上上签通知服务验签失败,请求信息不存在，rtick:{},sign:{}", rtick, sign);
            return null;
        }
        //业务结果
        HashMap resultBizMap = getParams(reqObj, request);
        log.info("【通知-验签】上上签通知服务信息:{}，rtick:{},sign:{}", resultBizMap, rtick, sign);
        //验证通过
        if (bestSignClient.signVerify(rtick, sign, JSON.toJSONString(resultBizMap))) {
            log.info("【通知-验签】上上签通知服务验签成功");
            return resultBizMap;
        }
        log.error("【通知-验签】上上签通知服务验签失败");
        return null;
    }

    /**
     * @param bizOrderNo   业务单号
     * @param resultBizMap 业务结果
     */
    @Override
    public void executeBiz(String bizOrderNo, HashMap resultBizMap) {
        log.info("【通知-处理】上上签通知服务信息,单号：{},结果：{}", bizOrderNo, resultBizMap);
        if (Objects.isNull(resultBizMap)) {
            return;
        }
        //行为
        String action = String.valueOf(resultBizMap.get("action"));
        switch (action) {
            case CERT_APPLY:
                contractService.notifyCertApply(resultBizMap);
                break;
            case CONTRACT_SIGN:
                //产品要求走短信验证，不走内部通知-注释掉
                //contractService.notifyContractSign(resultBizMap);
                break;
            default:
                break;
        }
    }


    @Override
    public void writerReturn(Boolean isSuccess, HttpServletResponse resp) {
        Map<String, Object> respMap = Maps.newHashMap();
        respMap.put("retCode", "0");
        respMap.put("retMsg", "通知接受成功");
        HtmlUtils.writerHtml(resp, isSuccess ? respMap : "FAILED");
    }


    /**
     * 遍历数据
     *
     * @param request 请求
     * @return 结果
     */
    private HashMap getParams(Object reqObj, HttpServletRequest request) {
        HashMap<String, Object> map = Maps.newHashMap();
        Map reqMap = request.getParameterMap();
        for (Object key : reqMap.keySet()) {
            String value = ((String[]) reqMap.get(key))[0];
            map.put(key.toString(), value);
        }
        if (reqMap.size() == 0) {
            return JSON.parseObject(JSON.toJSONString(reqObj), HashMap.class);
        }
        return map;
    }

}
